﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui.EquipementChambre
{
    /// <summary>
    /// Interaction logic for P_EquipementChambre.xaml
    /// </summary>
    public partial class P_EquipementChambre : Page
    {
        HotelClient srvService;
        Liste_Equipements _eqChambre;

        public P_EquipementChambre()
        {
            InitializeComponent();
        }

        private void gererListeEquipementChambre(object sender, RoutedEventArgs e)
        {

        }

        private void bt_add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Liste_Equipements eqChambre = new Liste_Equipements();
                eqChambre.Description_Equippement = tb_description.Text.Trim();

                //srvService = new HotelClient();
                if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                {
                    //writeToBusyTool("Connection au serveur...");
                    srvService.Open();
                }
                int resultatAjout = srvService.AddListeEquipements(eqChambre);

                if (resultatAjout > 0)
                {
                    MessageBox.Show("Enregistrement réussi",
                                    "Ajout Equipement pour Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                    loadListeEquipementChambre();
                }
                else
                {
                    MessageBox.Show("Erreur d'enresgistrement",
                                    "Ajout Equipement pour Chambre",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
                //srvService.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ajout Equipement pour Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //chargement de la liste des équipes pour chambre
        private void loadListeEquipementChambre()
        {
            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    writeToBusyTool("Chargement des équipements pour chambre en cours...");
                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_equipementChambre.DataContext = srvService.getListEquipements();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                        //srvService.Close();
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Equipement pour Chambre", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            busyTool.Width = SystemParameters.WorkArea.Width;
            busyTool.Height = SystemParameters.WorkArea.Height;

            //srvService = new HotelClient();
            #region etablissement connexion avec serveur

            try
            {

                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true; },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    srvService = new HotelClient();
                    //srvService.Open();

                    if (srvService.State == System.ServiceModel.CommunicationState.Closed || srvService.State == System.ServiceModel.CommunicationState.Closing)
                    {
                        writeToBusyTool("Connection au serveur...");
                        srvService.Open();
                    }

                    writeToBusyTool("Classement des équipements pour chambre...");
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                    (SendOrPostCallback)delegate
                                    {
                                        dg_equipementChambre.DataContext = srvService.getListEquipements();
                                    },
                                    null
                                    );


                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();
                //}

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            #endregion
        }
    }
}
